package com.gzc.just.play.last.war.sceneserver.benchmark;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.List;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

/**
 * 简单性能基准测试
 * 
 * 这是一个示例基准测试，演示基本的性能测试和验证
 * 
 * 运行方式:
 * mvn clean test -Dtest=SimpleBenchmarkTest
 */
public class SimpleBenchmarkTest {
    
    private List<Integer> list;
    private static final int SIZE = 1000;
    
    @BeforeEach
    public void setup() {
        list = new ArrayList<>();
        for (int i = 0; i < SIZE; i++) {
            list.add(i);
        }
    }
    
    @Test
    public void testListSetup() {
        assertNotNull(list, "List should not be null");
        assertEquals(SIZE, list.size(), "List should contain SIZE elements");
    }
    
    @Test
    public void testArrayListIteration() {
        int sum = 0;
        for (int i : list) {
            sum += i;
        }
        // 验证求和结果
        int expected = SIZE * (SIZE - 1) / 2;
        assertEquals(expected, sum, "Sum should match expected value");
    }
    
    @Test
    public void testArrayListStreamSum() {
        int sum = list.stream().mapToInt(Integer::intValue).sum();
        int expected = SIZE * (SIZE - 1) / 2;
        assertEquals(expected, sum, "Stream sum should match expected value");
    }
    
    @Test
    public void testSimpleArithmetic() {
        long result = 0;
        for (int i = 0; i < 1000; i++) {
            result += i * i;
        }
        // 验证求和结果 sum(i^2) for i=0 to 999 = 999*1000*1999/6
        long expected = 999L * 1000 * 1999 / 6;
        assertEquals(expected, result, "Arithmetic sum should match expected value");
    }
    
    @Test
    public void testStringConcatenation() {
        String result = "";
        for (int i = 0; i < 100; i++) {
            result += "value_" + i;
        }
        assertTrue(result.length() > 0, "String concatenation should produce non-empty result");
        assertTrue(result.contains("value_0"), "Result should contain first value");
        assertTrue(result.contains("value_99"), "Result should contain last value");
    }
    
    @Test
    public void testStringBuilder() {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < 100; i++) {
            sb.append("value_").append(i);
        }
        String result = sb.toString();
        assertTrue(result.length() > 0, "StringBuilder result should be non-empty");
        assertTrue(result.contains("value_0"), "Result should contain first value");
        assertTrue(result.contains("value_99"), "Result should contain last value");
    }
    
    @Test
    public void testStringBuilderVsConcatenation() {
        // 测试两种方式的结果应该相同
        String result1 = "";
        for (int i = 0; i < 50; i++) {
            result1 += "item_" + i;
        }
        
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < 50; i++) {
            sb.append("item_").append(i);
        }
        String result2 = sb.toString();
        
        assertEquals(result1, result2, "Both methods should produce identical results");
    }
    
    @Test
    public void testPerformanceMetrics() {
        // 性能验证 - 在合理时间内完成操作
        
        long startTime = System.nanoTime();
        int sum = 0;
        for (int i : list) {
            sum += i;
        }
        long iterationTime = System.nanoTime() - startTime;
        
        // 验证操作在1秒内完成
        assertTrue(iterationTime < 1_000_000_000L, 
            "List iteration should complete within 1 second, took " + iterationTime + " ns");
        
        startTime = System.nanoTime();
        long result = 0;
        for (int i = 0; i < 10000; i++) {
            result += i * i;
        }
        long arithmeticTime = System.nanoTime() - startTime;
        
        assertTrue(arithmeticTime < 1_000_000_000L, 
            "Arithmetic operations should complete within 1 second, took " + arithmeticTime + " ns");
    }
}
